﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using RLZY.others;
using Infragistics.Win.UltraWinGrid;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
namespace RLZY
{
    public partial class FormGZ_Print : Form
    {
        public FormGZ_Print()
        {
            InitializeComponent();
            
        }
        //public FormBDX_Modify(string username,String 权限)
        //{
        //    InitializeComponent();
        //    this.userID= strTextBox1Text;
        //}
        private static FormGZ_Print childFromInstanc;
        static log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public String userID;
        public String 权限;


        public static FormGZ_Print ChildFromInstanc
        {
            get
            {
                if (childFromInstanc == null || childFromInstanc.IsDisposed)
                //判断childFromInstanc是否有值，不为null或者窗体释放（当你将子窗体关了的时候）为childFromInstanc赋值
                {
                    childFromInstanc = new FormGZ_Print();
                }
                return childFromInstanc;
                //返回的childFromInstanc（子窗体存在时返回的是null,关闭了（IsDisposed为true）或者第一次打开子窗体返回一个ChildForm实例）
            }
        }
        public void Last_Date()
        {
            others.myGZ mygz = new myGZ();
            DataSet dataSet = mygz.GetLastDate();

            bool flag = dataSet.Tables[0].Rows.Count > 0;
            if (flag)
            {
                this.LastDate = dataSet.Tables[0].Rows[0]["日期"].ToString();
            }
            else
            {
                this.LastDate = "9998-12-31";
            }

        }

        private void FormNotKey_GDX_Modify_Load(object sender, EventArgs e)
        {
            BLL.userpassword bllpwd = new BLL.userpassword();
            Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
            others.myGZ mygz = new myGZ();
            权限 = model.备注;
        }

     

        private void BindData(DataSet ds)
        {
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            //dv.Sort = "ID desc";//反排序
            //bindingSource1.DataSource = dv;
            this.ultraGrid1.DataSource = dv;
        }
        //列选择器
        private void btnCustomColumnChooser_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowCustomColumnChooserDialog();
            }
            catch (Exception ex) { LOG.Error(ex); }
        }
        private RLZY.CustomColumnChooser customColumnChooserDialog = null;
        
        private void ShowCustomColumnChooserDialog()
        {
            if (null == this.customColumnChooserDialog || this.customColumnChooserDialog.IsDisposed)
            {
                this.customColumnChooserDialog = new RLZY.CustomColumnChooser();
                this.customColumnChooserDialog.Owner = this;
                this.customColumnChooserDialog.Grid = this.ultraGrid1;
            }

            this.customColumnChooserDialog.Show();
        }
        //修改列布局
        private void btnDisplayLayout_Click(object sender, EventArgs e)
        {
            ultraGrid1.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
        }
        //隐藏列
        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            //e.Layout.Bands[0].Columns[0].Hidden = true;
        }

        private void cmbBZ_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        
       
        
        //按回车键跳转

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter && !(ActiveControl is Button))
            {//碰到Button时不跳转到下一个控件，而是执行Button的单击事件
                keyData = Keys.Tab;
            }
            return base.ProcessDialogKey(keyData);
        }

       
        
        public string VarDat { get; set; }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                this.btnPrint.Enabled = false;
                others.gzPrint print = new gzPrint();
                DataSet ds = print.GetListGZ(this.权限,this.dtpDate.Text);
                this.BindData(ds);
                ToExcel();
                this.btnPrint.Enabled=true ;
              
            }
            catch (Exception ex) { LOG.Error(ex); }
           
        }

        private void ToExcel()
        {
            try
            {
                //模板文件
                string TempletFileName = "GZ.xls";
                //导出文件
                string ReportFileName = "C:\\GZ_JD_New.xls";
                FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read);
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);

                ISheet ws = hssfworkbook.GetSheet("GZ");
                //添加或修改WorkSheet里的数据


                #region
                //this.ultraGrid1.Rows[i].Cells[3].Value.ToString().Trim();
                int count= this.ultraGrid1.Rows.Count;
                int excelrows = 4;
                ws.GetRow(1).GetCell(23).SetCellValue(this.dtpDate.Text );//日期
                ws.GetRow(1).GetCell(3).SetCellValue(权限);//部门
                for (int i = 0; i < count;i++ )
                {

                    ws.GetRow(excelrows).GetCell(0).SetCellValue(this.ultraGrid1.Rows[i].Cells["用工性质"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(1).SetCellValue(this.ultraGrid1.Rows[i].Cells["车间班组"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(2).SetCellValue(this.ultraGrid1.Rows[i].Cells["姓名"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(3).SetCellValue(this.ultraGrid1.Rows[i].Cells["技能工资"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(4).SetCellValue(this.ultraGrid1.Rows[i].Cells["岗位工资"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(5).SetCellValue(this.ultraGrid1.Rows[i].Cells["绩效工资"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(6).SetCellValue(this.ultraGrid1.Rows[i].Cells["三项补贴"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(7).SetCellValue(this.ultraGrid1.Rows[i].Cells["回贴"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(8).SetCellValue(this.ultraGrid1.Rows[i].Cells["工龄津贴"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(9).SetCellValue(this.ultraGrid1.Rows[i].Cells["职称津贴"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(10).SetCellValue(this.ultraGrid1.Rows[i].Cells["特岗津贴"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(11).SetCellValue(this.ultraGrid1.Rows[i].Cells["行车工津贴"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(12).SetCellValue(this.ultraGrid1.Rows[i].Cells["卫生纸费"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(13).SetCellValue(this.ultraGrid1.Rows[i].Cells["中晚班费"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(14).SetCellValue(this.ultraGrid1.Rows[i].Cells["加班工资"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(15).SetCellValue(this.ultraGrid1.Rows[i].Cells["补发"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(16).SetCellValue(this.ultraGrid1.Rows[i].Cells["扣事假工资"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(17).SetCellValue(this.ultraGrid1.Rows[i].Cells["扣病假工资"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(18).SetCellValue(this.ultraGrid1.Rows[i].Cells["其它扣款"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(19).SetCellValue(this.ultraGrid1.Rows[i].Cells["应发"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(20).SetCellValue(this.ultraGrid1.Rows[i].Cells["扣失业保险"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(21).SetCellValue(this.ultraGrid1.Rows[i].Cells["扣养老金"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(22).SetCellValue(this.ultraGrid1.Rows[i].Cells["扣医疗保险"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(23).SetCellValue(this.ultraGrid1.Rows[i].Cells["扣公积金"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(24).SetCellValue(this.ultraGrid1.Rows[i].Cells["扣电视费"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(25).SetCellValue(this.ultraGrid1.Rows[i].Cells["扣治安费"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(26).SetCellValue(this.ultraGrid1.Rows[i].Cells["扣会费"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(27).SetCellValue(this.ultraGrid1.Rows[i].Cells["扣互助金"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(28).SetCellValue(this.ultraGrid1.Rows[i].Cells["扣丧葬费"].Value.ToString().Trim()); 
                    ws.GetRow(excelrows).GetCell(29).SetCellValue(this.ultraGrid1.Rows[i].Cells["实发"].Value.ToString().Trim()); 
                    //ws.GetRow(excelrows).GetCell(23).SetCellValue(this.ultraGrid1.Rows[i].Cells["扣会费"].Value.ToString().Trim()); 

                    excelrows++;
                }
                
                #endregion
                ws.ForceFormulaRecalculation = true;

                using (FileStream filess = File.OpenWrite(ReportFileName))
                {
                    hssfworkbook.Write(filess);
                }
                System.Diagnostics.Process.Start("explorer.exe", @"C:\GZ_JD_New.xls");
            }
            catch (Exception ex) { LOG.Error(ex); }

            //System.Diagnostics.Process.Start(Application.StartupPath + "C:\\RLZY_Prn\\SJTZS_Tmp_New.xls");
        }

        public string LastDate { get; set; }

        private void FormNT_Show_FormClosing(object sender, FormClosingEventArgs e)
        {
            others.RYFB fb = new RYFB();
            fb.DropGZ1();
        }
    }
}
